﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;

namespace BackToCOS
{
    public class COSAPIClient
    {
        #region 获取签名

        /// <summary>
        /// 获取签名
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="secretId"></param>
        /// <param name="secretKey"></param>
        /// <param name="expiredSecond">单位秒</param>
        /// <param name="fileId"></param>
        /// <param name="bucketName"></param>
        /// <returns></returns>
        public static string GetSign(int appId, string secretId, string secretKey, long expiredSecond, string fileId, string bucketName)
        {
            if (secretId == "" || secretKey == "")
            {
                throw new Exception("secretId或secretKey不允许为空");
            }
            var dtNow = DateTime.Now;
            var now = Util.ToUnixTime(dtNow);
            var expired = Util.ToUnixTime(dtNow.AddSeconds(expiredSecond));
            var rand = new Random();
            var rdm = rand.Next(Int32.MaxValue);
            var signText = "a=" + appId + "&k=" + secretId + "&e=" + expired + "&t=" + now + "&r=" + rdm + "&f=" + fileId + "&b=" + bucketName;
            return Util.HmacSha1(secretKey, signText);
           
        }
        #endregion

      
    }

}

